<template>
  <div id="bbs">
    <!-- <el-row>
   
    <el-card :body-style="{ padding: '0px'   }" style=" width: 300px;
            height: 300px;
            margin: 0 auto; ">
      <img src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png" class="image">
      <div style="padding: 14px;">
        <span>好吃的汉堡</span>
        <div class="bottom clearfix">
          <time class="time">{{ currentDate }}</time>
          <el-button type="text" class="button">操作按钮</el-button>
        </div>
      </div>
    </el-card>
  </el-col>
    </el-row>-->
    <div style="text-align:right;">
      <el-button
        type="primary"
        icon="el-icon-circle-plus"
        @click="opendialogForm()"
        style="margin-right: 50px;"
      >写文章</el-button>
    </div>

    <el-dialog title="发表文章" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="文章标题 :" >
          <el-input v-model="form.artTitle"  autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="文章图片 :">
          <input
            type="file"
            ref="fileInt"
            accept="image/png, image/gif, image/jpeg"
            @change="getFile"
          />
        </el-form-item>

        <el-form-item label="文章内容 :">
          <el-input type="textarea" v-model="form.artContent"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm($event)">确 定</el-button>
      </div>
    </el-dialog>

    <el-row>
      <el-col
        :span="12"
        v-for="(o) in BbsArticle"
        :key="o.id"
        style="margin-top:20px; padding: 14px;"
      >
        <div class="grid-content bg-purple-dark">
          <el-card :body-style="{ padding: '20px' }">
            <router-link :to="'/bbs/bbsDetails/'+o.id">
              <img
                :src="'yjl/download/'+o.imageUrl"
                class="image"
              />
            </router-link>
            <div style="padding: 14px; width:'80%'">
              <span>{{o.artTitle}}</span>

              <div class="bottom clearfix">
                <el-row>
                  <el-col :span="12" style>
                    <div class="grid-content bg-purple">
                      <i class="el-icon-s-comment" @click="commentBbs(o.id)">
                        评论
                        <span>({{o.artComNum}})</span>
                      </i>
                    </div>
                  </el-col>
                  <el-col :span="12">
                    <div class="grid-content bg-purple-light">
                      <i class="el-icon-position" @click="likeBbs(o.id)">
                        点赞
                        <span>({{o.artLikeNum}})</span>
                      </i>
                    </div>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";
import http from "../../utils/https";

export default {
  name: "Bbs",
  data() {
    return {
      keys: "",
    
      o: "",
      dialogFormVisible: false,
      currentDate: new Date(),
      form: {
        name: "",
        region: "",
        file: ""
      },

      BbsArticle: [
        {
          id: "",
          artUserId: "",
          artTitle: "",
          artTypeId: "",
          artContent: 0,
          artCreTime: "",
          artView: "",
          artComNum: "",
          artLikeNum: ""
        }
      ],
      Comment: {
        bbsArticleId: "",
        detile: ""
      }
    };
  },

  beforeCreate() {
    console.log("bbs1");
  },

  created() {
    console.log("bbs2");
  },

  beforeMount() {
    console.log("bbs3");
  },

  mounted() {
    this.initList();
  },

  beforeUpdate() {
    console.log("bbs5");
  },

  updated() {
    console.log("bbs6");
  },

  beforeDestroy() {
    console.log("bbs7");
  },

  destroyed() {
    console.log("bbs8");
  },

  activated() {
    console.log("bbs9");
  },

  deactivated() {
    console.log("bbs10");
  },

  methods: {
    getFile(event) {
      console.log(this)
      console.log(event.currentTarget.files)
      this.form.file = event.currentTarget.files[0];
     // this.form.file = this.$refs.fileInt.files[0];
      console.log(this.form.file);
    },

    opendialogForm(){
       if (this.$store.state.isLogin) {
        this.$message.error("请先登录");
        return;
      }
      this.dialogFormVisible = true;
    },

    submitForm(event) {
      let that = this;
      event.preventDefault();
      let formData = new FormData();
      formData.append("artTitle", this.form.artTitle);
      formData.append("artContent", this.form.artContent);
      formData.append("file", this.form.file);

      let config = {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      };

      axios
        .post("/yjl/bbs-article/upload", formData, config)
        .then(function(res) {
             console.log(res.data);
            let code = res.data.code;
          let msg = res.data.msg;
          if (code != 200) {
          that.$message.error(msg);
        } else {
          that.dialogFormVisible = false;
        
          that.$message({
            message: msg,
            type: "success"
          });
        }
        });
    },

    initList() {
      http
        .get({
          url: "/yjl/bbs-article"
        })
        .then(res => {
          if (res.code == 200) {
            console.log(res.content);
            this.BbsArticle = res.content;
          } else {
            alert(接口异常);
          }
        });
    },

    likeBbs(e) {
      console.log(e);
      axios({
        method: "put",
        url: "/yjl/bbs-article/like/" + e
      }).then(res => {
        console.log(res.data);
        let code = res.data.code;
        let msg = res.data.msg;
        if (code != 200) {
          this.$forceUpdate();
          this.initList();
          this.$message.error(msg);
        } else {
          this.$forceUpdate();
          this.initList();
          this.$message({
            message: msg,
            type: "success"
          });
        }
      });
    },

    commentBbs(e) {
      if (this.$store.state.isLogin) {
        this.$message.error("请先登录");
        return;
      }

      let id = e;
      console.log(e);
      this.$prompt("请输入你的评论", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          console.log(value);
          console.log(id);

          http
            .postJson({
              url: "/yjl/comment/autho",
              data: {
                bbsArticleId: e,
                detile: value
              }
            })
            .then(res => {
              console.log(res);
              if (res.code != 200) {
                this.$forceUpdate();
                this.initList();
                this.$message.error(res.msg);
              } else {
                this.$forceUpdate();
                this.initList();
                this.$message({
                  message: res.msg,
                  type: "success"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    }
  },

  computed: {}
};
</script>

<style scoped>
#bbs {
  padding-left: 20px;
  padding-right: 20px;
}
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  height: 400px;

  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style>
